#!/usr/bin/env python3
#coding=utf-8

import os, sys, getopt


server_ip="dev8.cn"
server_path="/opt/dev8/web"
shell_name=""
app_jar_name="dist/*"
name="dev8.cn"
process=[]

def setWorkPath():
    base_path =  os.path.abspath( os.path.join( __file__ ,"../" ) )
    os.chdir( base_path)


def p(code):
    if( code >> 8 ):
        print()
        print("系统出现错误!")
        sys.exit(code)

def proc_crlf_work(file):
    lines = []
    with open(file, 'r', encoding='UTF-8') as fh:
        fh.seek(0)
        for line in fh.readlines():
            lines.append(line.replace("\r\n","\n"))

    with open(file, 'wb') as fh:
        fh.write(bytes("".join(lines),encoding="utf8"))
        fh.flush()
    print(file)

def proc_crlf(path):
    for file in os.listdir(path):
        if file in ["node_modules","dist"]:
            continue
        fullName = os.path.join(path,file)
        if( os.path.isfile(fullName)):
            if not file.endswith(".sh"):
                continue
            proc_crlf_work(fullName)
        else:
            proc_crlf(fullName)

if __name__=='__main__':
    setWorkPath()

    #如果扩展名是 .sh，去除\r
    proc_crlf(os.path.abspath('.'))
    cmd = "npm run build"
    print(cmd)
    p(os.system(cmd))

    cmd = '''scp -r %s root@%s:%s/''' % (app_jar_name, server_ip,server_path)
    print(cmd)
    p(os.system(cmd))


    print('''%s 发布完成'''%(name))

